package com.novabluetooth.services.servicedevices;

import java.io.Serializable;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

import com.novabluetooth.services.TJBluetoothLeService;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.os.SystemClock;
import android.util.Log;

/**
 * 科瑞康血氧
 * 
 * @author nova
 *
 */
public class XueYangKRKService extends TJBluetoothLeService {
//implements Serializable 
//	private static class SingletonHolder {
//		/**
//		 * 单例对象实例
//		 */
//		static final XueYangKRKService INSTANCE = new XueYangKRKService();
//	}
//
//	public static XueYangKRKService getInstance() {
//		return SingletonHolder.INSTANCE;
//	}
//
//	/**
//	 * private的构造函数用于避免外界直接使用new来实例化对象
//	 */
//	private XueYangKRKService() {
//	}
//
//	/**
//	 * readResolve方法应对单例对象被序列化时候
//	 */
//	private Object readResolve() {
//		return getInstance();
//	}

	// 力康POD-1W血氧
	private String POD_readData = "AA550F03840157";
	private String POD_closeDevice = "AA550F03840050";
	/**
	 * 力康(科瑞康)血氧仪
	 */
	public final static UUID podServiceUUID = UUID.fromString("0000ffb0-0000-1000-8000-00805f9b34fb");
	// public final static UUID writeCharactUUID = UUID
	// .fromString("0000fff1-0000-1000-8000-00805f9b34fb");
	public final static UUID podWRCharactUUID = UUID.fromString("0000ffb2-0000-1000-8000-00805f9b34fb");
	public Timer timer4 = new Timer();
	int readCount = 0;

	@Override
	public void servicesDiscovered() {
		super.servicesDiscovered();
		Log.e(TAG, "TJ_LIKANGXUEYANG 111111 ");
		new Thread() {
			public void run() {
				BluetoothGattService gattService1 = mBluetoothGatt.getService(podServiceUUID);
				muGattCharact = gattService1.getCharacteristic(podWRCharactUUID);
				boolean success1 = mBluetoothGatt.setCharacteristicNotification(muGattCharact, true);
				Log.d(TAG, "notification enabledescriptor:" + success1);
				Log.e(TAG, "TJ_LIKANGXUEYANG 111111 ");

				List<BluetoothGattDescriptor> descriptors2 = muGattCharact.getDescriptors();
				for (BluetoothGattDescriptor dp1 : descriptors2) {
					dp1.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
					boolean success3 = mBluetoothGatt.writeDescriptor(dp1);
					Log.d(TAG, "ENABLE_NOTIFICATION_VALUE:" + success3);
					if (!success3) {
						SystemClock.sleep(1000);
						dp1.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
						success3 = mBluetoothGatt.writeDescriptor(dp1);
						Log.d(TAG, "ENABLE_NOTIFICATION_VALUE2:" + success3);
					}
				}

			};
		}.start();
		timer4.schedule(new TimerTask() {

			@Override
			public void run() {
				writeDataToPODDevice(POD_readData);
				// writeDataToDevice(BPM_closeSound);
				Log.e(TAG, "POD 111111 ");

			}
		}, 3500);
	}

	@Override
	public boolean isRightData(byte[] buffer) {
		super.isRightData(buffer);
		if (buffer.length > 10) {// AA 55 0F 08 01 5E 39 00 4B 02 1C 49 10
			// 6F E8 04

			if (((buffer[3] & 0xFF) == 8) && ((buffer[4] & 0xFF) == 1)) {

				int xueYang = buffer[5] & 0xFF;
				int num2 = (buffer[6] & 0xff) + ((buffer[7] & 0xff) << 8);
				// int num = buffer[6] & 0xFF + buffer[7] & 0xFF * 10;

				if (xueYang > 0)
					readCount++;

				if (readCount <= 5)
					return false;

				checkData = xueYang + "/" + num2;
				ifSuccess = true;
			}
		}

		return ifSuccess;

	}

	@Override
	public void closeDevice() {
		writeDataToPODDevice(POD_closeDevice);
		timer4.cancel();
	}

	public void writeDataToPODDevice(final String data) {
		new Thread() {
			public void run() {
				Log.e(TAG, "POD gattService1 ");
				if (mBluetoothGatt == null) {
					System.out.println("mBluetoothGatt==null******************************");
					return;
				}
				BluetoothGattService gattService1 = mBluetoothGatt.getService(podServiceUUID);

				BluetoothGattCharacteristic chara = gattService1.getCharacteristic(podWRCharactUUID);

				byte[] toD = String2Byte(data);
				chara.setValue(toD);
				chara.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
				boolean ifSuccess = mBluetoothGatt.writeCharacteristic(chara);
				Log.e(TAG, "POD gattService1 " + ifSuccess);
			}
		}.start();

	}

}
